﻿(function() {
	var LID;
	function init() {
		var s, head, btn, confirm_btn;
		btn = document.createElement("div");
		btn.className = "touzhu2";
		btn.onclick = function() {
			refreshd($("#LID").val());
		};
		btn.textContent = "投注";
		confirm_btn = $(".border_out1_r");
		confirm_btn.append(btn);
		$(".touzhu2").eq(1).remove();
	}

	function clear() {
		$(".img_bt1").css("background-image", "url(/img/pg28/img_bt1.gif)");
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				$(this).val("");
			}
		});
		$("#panel").find("input[name='checkboxd']").attr("checked", false);
		$("#totalvalue").text("0");
	}
	//数字加千分符号
	function ver(n) {
		re = /(\d{1,3})(?=(\d{3})+(?:$|\.))/g
		return n.replace(re, "$1,")
	}

	var first = 0;
	//取总的投注金蛋
	function getAllpceggs() {
		var total = 0;
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				var txt_value = $.trim($(this).val()).replace(/,/gi, "");
				if (txt_value && !isNaN(txt_value)) {
					total += parseInt(txt_value);
				}
			}
		})

		$("#totalvalue").text(ver(total + ""));

	}

	function setAllvalue(peilv) {
		$("#panel").find("input[name='SMONEY']").each(function() {
			if (!$(this).attr("readonly")) {
				var txt_value = $.trim($(this).val()).replace(/,/gi, "");
				if (txt_value && !isNaN(txt_value)) {
					var new_value = Math.floor(txt_value * peilv);
					$(this).val(ver(new_value + ""));
				}
			}
		});
	}
	function isCon(arr, val) {
		for (var i = 0; i < arr.length; i++) {
			if (arr[i] == val) return true;
		}
		return false;
	}
	function refreshd() {
		var qihao = $("#RemainTitle").prev().text().replace("期开奖结果:","").trim();
		var url = "http://127.0.0.1:8010/dandan/GetPredictionFrom?qihao=" + (parseInt(qihao)+1);
		if(localStorage.getItem("ds")!=null)
			url +="&ds=" + encodeURIComponent( localStorage.getItem("ds"));
		if(localStorage.getItem("ds_f")!=null)
			url +="&ds_f=" +encodeURIComponent( localStorage.getItem("ds_f"));
		if(localStorage.getItem("dx")!=null)
			url +="&dx=" + encodeURIComponent( localStorage.getItem("dx"));
		if(localStorage.getItem("dx_f")!=null)
			url +="&dx_f=" + localStorage.getItem("dx_f");
		if(localStorage.getItem("dx_wei")!=null)
			url +="&dx_wei=" + encodeURIComponent( localStorage.getItem("dx_wei"));
		if(localStorage.getItem("dx_wei_f")!=null)
			url +="&dx_wei_f=" + localStorage.getItem("dx_wei_f");
		var xhr = new XMLHttpRequest();
		xhr.open("GET", url, true);
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				clear();
				var result = eval('(' + xhr.responseText + ')');
				localStorage.setItem('ds',result.ds.value);
				localStorage.setItem('ds_f',result.ds.flag.split("-")[0]);
				localStorage.setItem('dx',result.dx.value);
				localStorage.setItem('dx_f',result.dx.flag.split("-")[0]);
				localStorage.setItem('dx_wei',result.dx_wei.value);
				localStorage.setItem('dx_wei_f',result.dx_wei.flag.split("-")[0]);
				var c = 0 ;
				for(var i = 0 ; i < 28 ; i++){
					var p = getPeilv(i,result.ds,"ds");
					p+= getPeilv(i,result.dx,"dx");
					p+= getPeilv(i,result.dx_wei,"dx_wei");
					if(p>0){
						c++;
						var index = i>13?27-i+14:i;
						$("#panel td img").eq(index).trigger('click');
						var txt=$("#panel td img").eq(index).parent().parent().find("input[name='SMONEY']");
						 if(!txt.attr("readonly")){
			                      var  txt_value=txt.val().replace(/,/gi,"");
								var new_value=Math.floor(txt_value*p);
								txt.val(ver(new_value+""));
								getAllpceggs();
							}
					}
				}
				if(c == 0 ){
					for(var i = 0 ; i < 28 ; i++){
						$("#panel td img").eq(index).trigger('click');
					}
				}
				comform();
			}
		}
		xhr.send();
	}
	function getPeilv(v,re,f){
		var peilv = 0;
		if(parseInt(re.flag.split("-")[0])<2){ 
			if(f=="ds"){
				if((v%2==0&&re.value=="双")||(v%2==1&&re.value=="单"))
						peilv = parseInt(re.flag.split("-")[1]);
			}else if(f=="dx"){
				if((v>13&&re.value=="大")||(v<14&&re.value=="小"))
						peilv = parseInt(re.flag.split("-")[1]);
			}else if(f="dx_wei"){
				if((v%10>4&&re.value=="大")||(v%10<5&&re.value=="小"))
						peilv = parseInt(re.flag.split("-")[1]);
			}
		}
		return peilv;
	}
	function task() {
		refreshd();
	}
	function comform() {
		t = $("#totalvalue").text().replace(/,/gi, "");
		t = parseInt(t);
		var str = [];
		for (var i = 0; i < 28; i++) {
			var txt_value = $.trim($("#txt" + i).val()).replace(/,/gi, "");
			str.push(txt_value);
		}
		$("#ALLSMONEY").val(str.join(","));
		t = ver(String(t)); //将数字转字符串后千分位 
		$("#postgoldeggs").html(t);
		$("#SMONEYSUM").val(t);
		$("input[name='SMONEY']").attr("disabled", false);

		var form1 = document.getElementById("form1"); //父层
		form1.submit();
	}
		function locationTo() {
			window.location.href = "http://www.pceggs.com/play/pg28Insert.aspx?LID=" + LID;
		}
	 var url = document.URL;
	 if (url.indexOf('http://www.pceggs.com/play/pg28Insert.aspx?') != -1) {
			init();
			setInterval(refreshd, (20+Math.random()*60)*1000);
	}else if (url.indexOf('http://www.pceggs.com/play/pc28.aspx') != -1) {
			var tds = $("#panel").find("tr:eq(5)").find("td");
			var _status = tds.eq(7).find("script").eq(0).text().trim().split("', '")[2].trim().replace("');",'');
			if(_status==0){
				LID = tds.eq(7).find("script").eq(0).text().trim().split("', '")[1].trim();
				setInterval(locationTo, 5000);
			}
	}
})();